<?php
$items = array();

$brief_lbl = BUtils::T('merchant_brief_lbl');
$offers_lbl = BUtils::T('offers_lbl');
$rewards_lbl = BUtils::T('rewards_lbl');
$see_detail_lbl = BUtils::T('see_detail_lbl');

$default_logo = Yii::app()->baseUrl . '/img/default_special_thumb.png';

$html = '';
$markers = array();
$total = count($rows);
if ($total == 0) {
	$html = <<<HTML
<ul>
	<li class="card noResults">
		<h4>Hmm... Try:</h4>
		<p>Whoops! Cannot find any result.</p>
		<ul class="suggestions" style="display:none;">
			<li class="zoomSuggestion"><span data-action="zoomOut" class="link">Zooming out</span> to search a bigger area</li>
			<li>Searching for something more general (like <span data-value="food" data-action="category" class="link">Food</span>, <span data-value="coffee" data-action="category" class="link">Coffee</span>, or <span data-value="drinks" data-action="category" class="link">Shops</span>)</li>
			<li>Checking your spelling</li>
		</ul>
	</li>
<ul>
HTML;
} else {
	$html = '<ul>';
	foreach ($rows as $k => $r) {
		$features = array();
		if (!empty($r['cuisine_type'])) {
			$array = explode(',', $r['cuisine_type']);
			foreach ($array as $item) {
				$features[] = array(
					'key' => $item,
					'name' => BUtils::T($item),
				);
			}
		}

		if (empty($r['seo_id'])) {
			$bizLink = Yii::app()->createUrl($this->id.'/view', array('id'=>$r['id']));
		} else {
			$bizLink = Yii::app()->createUrl($this->id.'/view', array('id'=>$r['seo_id']));
		}
		$bizLogoLink = BUtils::GetBusinessPictureUrl($r['url_logo']);

		//set the tags: 3 tags max
		$bizTagLinks = trim($this->getTagLinks($r['cuisine_type'], '<span class="delim"> • </span>', 3));
		if (!empty($bizTagLinks)) {
			$bizTagLinks = '<i class="icon-tags"></i> ' . $bizTagLinks;
		}
		
		//set address
		$address = trim($r['address']);
		
		//set neighbourhood
		$neighbourhood_info = '';
		if (!empty($r['biz_area'])) {
			$arr = explode(',', $r['biz_area']);
			foreach ($arr as $n) {
				$nh = trim($n);
				if (!empty($nh)) {
					$neighbourhood_info .= '<span class="categoryName">' . $nh . '</span> ';
				}
			}
		}
		
		//business icon url
		$biz_icon_url = Yii::app()->baseUrl . '/img/icons/icon_others.png';
		if (!empty($r['category_key'])) {
			$biz_icon_url = Yii::app()->baseUrl . '/img/icons/icon_'.$r['category_key'].'.png';
		}

		//set the offers
		$offers_block = null;
		if (!empty($r['offers']) and $r['offers'] > 0) {
			$offerListLink = Yii::app()->createUrl('/special/view', array('id'=>$r['id_special']));
			$offers_block = <<<HTML
<div class="specialInfo" data-url="{$offerListLink}">
	<h5 class="type"><i class="icon-certificate"></i> {$offers_lbl}</h5>
	<div class="message">
		<p>Find {$r['offers']} offer(s) here!</p>
	</div>
</div>
HTML;
		}
		
		//set the rewards
		$rewards_block = null;
		if (!empty($r['rewards']) and $r['rewards'] > 0) {
			$rewards_block = <<<HTML
<div class="rewardsInfo" data-url="{$bizLink}">
	<h5 class="type"><i class="icon-trophy"></i> {$rewards_lbl}</h5>
	<div class="message">
		<p>Find {$r['rewards']} reward(s) here!</p>
	</div>
</div>
HTML;
		}
		
		//set brief
		$brief = strip_tags(trim($r['brief']));
		
		//set distance
		$distance = '';
		if (!empty($r['distance'])) {
			$distance = sprintf('%.1f', $r['distance']);
			if ($distance < 1.0) {
				$distance = sprintf('%d m', $distance * 100);
			} else {
				$distance = $distance . ' km';
			}
			$distance = '<div class="button-icon locate active"><i class="icon-location-arrow"></i> '.$distance.'</div>';
		}
		
		//fb like button
		$like_btn = '<div class="fb-like" data-href="http://yamii.fi'.$bizLink.'" data-width="100" data-layout="button_count" data-show-faces="false" data-send="false"></div>';

		$pinId = 'mid_' . $r['id'];
		$pinIndex = $total - $k - 1;
		$html .= <<<HTML
<li class="card" id="{$pinId}" data-pin-id="{$pinId}" data-pin-i="{$pinIndex}">
	<div class="venueBlock">
		<div class="venueDetails">
			<div class="venueName">
				<a href="{$bizLink}">{$r['name']}</a>
			</div>
			<div class="venueIcon">
				<img src="{$biz_icon_url}" />
			</div>
			<div class="venueAddressData">
				<div class="venueAddress">{$address}</div>
				<div class="venueData">
					{$neighbourhood_info}
				</div>
			</div>
			<div class="clearfix"></div>
		</div>
	</div>
	<div class="photoContainer">
		<a href="{$bizLink}">
			<img width="100" height="100" alt="" src="{$bizLogoLink}" onError="this.onerror=null;this.src='{$default_logo}';" />
		</a>
	</div>
	{$rewards_block}
	{$offers_block}
	<div class="detail phrases">
		<h5 class="type">{$brief_lbl}</h5>
		<ul class="phrases">
			<li class="phrase">
				<div class="brief-text">{$brief}</div>
			</li>
		</ul>
	</div>
	<div class="resultFooter">
		<div class="marker">
			{$bizTagLinks}
		</div>
		<div class="buttons">
			<div class="button-icon inactive">
				
			</div>
			{$distance}
		</div>
	</div>
<li>
HTML;

		//create markers
		$bizSpecialsMarker = null;
		if (!empty($r['offers']) and $r['offers'] > 0) {
			$bizSpecialsMarker = '<span class="specialsTab"><a class="link">'.$r['offers'].'</a></span>';
		}
		$marker = array(
			'id' => $r['id'],
			'lat' => $r['latitude'],
			'lng' => $r['longitude'],
			'status' => $r['status'],
			'category' => $r['category_key'],
			'offers' => (int)$r['offers'],
		);
		$marker['body'] = <<<HTML
<div class="place-sider clearfix relative">
	{$bizSpecialsMarker}
	<div class="bizInfo">
		<div class="name">{$r['name']}</div>
		<div class="address">
			<i class="gicon-google-maps gicon-white"></i> <span class="street">{$r['address']}</span><br />
			<i class="gicon-blank"></i> <span class="city">{$r['postcode']} {$r['city']}</span>
		</div>
HTML;
		if (!empty($r['phone'])) {
			$marker['body'] .= '<div class="phone"><i class="gicon-phone gicon-white"></i> '.$r['phone'].'</div>';
		}
		$marker['body'] .= '<div class="see-detail"><a href="'.$bizLink.'">'.$see_detail_lbl.'</a></div>';
		$marker['body'] .= '</div></div>';
		$markers[] = $marker;

		$items[] = array(
			'venue' => array(
				'id' => $r['id'],
				'name' => $r['name'],
				'logo' => $r['url_logo'],
				'phone' => $r['phone'],
				'category' => $r['category_key'],
				'location' => array(
					'address' => $r['address'],
					'lat' => $r['latitude'],
					'lon' => $r['longitude'],
					'postalCode' => $r['postcode'],
					'city' => $r['city'],
					'cc' => $r['country_code'],
				),
				'tags' => $features,
				'offers' => (int)$r['offers'],
			),
		);
	}
	$html .= '<li class="card noResults" style="min-height:20px;text-align:center; display:none;">
		<span>More merchants</span>
	</li>
	</ul>';
}
echo json_encode(array(
	'number' => count($items),
	'items' => $items,
	'html' => $html,
	'markers' => array_reverse($markers),//let the merchant with offers in the last
));